package xcmg.device.service.report;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.report.BackMoneyDO;
import xcmg.device.dao.mapper.report.BackMoneyMapper;
import xcmg.device.infra.ExportUtil;
import xcmg.device.service.vo.report.BackMoneyConditionVO;
import xcmg.device.service.vo.report.BackMoneyVO;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Service
public class BackMoneyService {

    @Autowired
    private BackMoneyMapper backMoneyMapper;

    @Autowired
    private HttpServletResponse response;

    public PageCommonVO pageList(SearchCommonVO<BackMoneyConditionVO> conditionVO, String orgId) {
        conditionVO.getFilters().setOrgId(orgId);
        PageHelperPlus.startPage(conditionVO.getPageNum(), conditionVO.getPageSize());
        List<BackMoneyDO> backMoneyDOs = backMoneyMapper.list(conditionVO.getFilters());
        PageCommonVO pageCommonVO = new PageCommonVO();
        pageCommonVO.setPageInfo(new PageInfo(backMoneyDOs));
        processDO(backMoneyDOs);
        List<BackMoneyVO> backMoneyVOs = new ArrayList<BackMoneyVO>();
        for (BackMoneyDO backMoneyDO : backMoneyDOs) {
            BackMoneyVO backMoneyVO = new BackMoneyVO();
            BeanUtils.copyProperties(backMoneyDO, backMoneyVO);
            backMoneyVOs.add(backMoneyVO);
        }
        pageCommonVO.setPageInfoList(backMoneyVOs);
        return pageCommonVO;
    }

    private void processDO(List<BackMoneyDO> backMoneyDOs) {
        DateFormat monthFormat = new SimpleDateFormat("yyyy-MM");
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        for (BackMoneyDO backMoneyDO : backMoneyDOs) {
            if (backMoneyDO.getBackTime() != null) {
                backMoneyDO.setBackMonthStr(monthFormat.format(backMoneyDO.getBackTime()));
                backMoneyDO.setBackDateStr(dateFormat.format(backMoneyDO.getBackTime()));
                cal.setTime(backMoneyDO.getBackTime());
                int date = cal.get(Calendar.DATE);
                if (date < 10) {
                    backMoneyDO.setDurationStr("上旬");
                } else if (date < 20) {
                    backMoneyDO.setDurationStr("中旬");
                } else {
                    backMoneyDO.setDurationStr("下旬");
                }
            }
            if (backMoneyDO.getBillTime() != null) {
                backMoneyDO.setBillTimeStr(timeFormat.format(backMoneyDO.getBillTime()));
                backMoneyDO.setBillState(Integer.valueOf(1));
            } else {
                backMoneyDO.setBillState(Integer.valueOf(0));
            }
            if (backMoneyDO.getBillTime() != null && backMoneyDO.getPayTime() != null) {
                cal.setTime(backMoneyDO.getPayTime());
                int endMonth = cal.get(Calendar.MONTH);
                int endYear = cal.get(Calendar.YEAR);
                cal.setTime(backMoneyDO.getBillTime());
                int startMonth = cal.get(Calendar.MONTH);
                int startYear = cal.get(Calendar.YEAR);
                int duration = (endYear - startYear) * 12 + (endMonth - startMonth);
                if (duration > 0) {
                    backMoneyDO.setBackDurationStr("往期");
                } else {
                    backMoneyDO.setBackDurationStr("当期");
                }
            }
            if (backMoneyDO.getPayTime() != null) {
                backMoneyDO.setPayTimeStr(timeFormat.format(backMoneyDO.getPayTime()));
            }
        }
    }

    public void export(BackMoneyConditionVO condition, String orgId) throws Exception {
        condition.setOrgId(orgId);
        List<BackMoneyDO> doList = backMoneyMapper.list(condition);
        processDO(doList);
        String title = "备件回款明细";
        String[] rowsName = new String[]{"回款月份", "上中下旬", "回款日期", "客户名称", "NC客户编码", "PMS客户编码", "客户类型", "回款金额（元）", "回款方式", "打款名称",
                "打款时间", "承兑票号", "回款期限（当期/往期）", "业务单号", "是否开票", "开票时间", "备注"};
        List<Object[]> dataList = new ArrayList<Object[]>();
        Object[] objs;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < doList.size(); i++) {
            BackMoneyDO e = doList.get(i);
            objs = new Object[rowsName.length];
            objs[0] = e.getBackMonthStr();
            objs[1] = e.getDurationStr();
            objs[2] = e.getBackDateStr();
            objs[3] = e.getCustomName();
            objs[4] = e.getCustomNcCode();
            objs[5] = e.getCustomPmsCode();
            objs[6] = e.getCustomType();
            objs[7] = e.getBackMoney();
            objs[8] = e.getBackType();
            objs[9] = e.getPayer();
            objs[10] = e.getPayTimeStr();
            objs[11] = e.getBillNo();
            objs[12] = e.getBackDurationStr();
            objs[13] = e.getBillNo();
            objs[14] = Integer.valueOf(1).equals(e.getBillState())?"是":"否";
            objs[15] = e.getBillTimeStr();
            objs[16] = e.getRemark();
            dataList.add(objs);
        }
        ExportUtil exportUtil = new ExportUtil(title, rowsName, dataList);
        exportUtil.export(response);
    }
}
